package com.tiance.demo.ui.property;

import com.tiance.demo.observer.ObserverData;
import com.tiance.demo.observer.ObserverEvent;
import com.tiance.demo.observer.ObserverSystem;
import com.tiance.demo.observer.Publisher;
import com.tiance.demo.service.mysql.bo.Page;
import com.tiance.demo.service.mysql.bo.pageindex.IndexPageHeader;
import com.tiance.demo.service.mysql.bo.pageindex.PageIndex;
import com.tiance.demo.service.mysql.bo.pageundolog.PageUndoLog;
import com.tiance.demo.service.mysql.bo.pageundolog.UndoLogPageHeader;
import com.tiance.demo.ui.vo.ByteItem;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.util.Observable;
import java.util.Observer;

public class PageHeaderPropertyTable extends TableView<ByteItem> implements Observer {

    public PageHeaderPropertyTable() {
        setPrefHeight(500);
        setEditable(false);
        getSelectionModel().setSelectionMode(SelectionMode.SINGLE); //多选模式
        setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);//列居左

        TableColumn propKey = new TableColumn("Property");
        TableColumn propValue = new TableColumn("Value");
        TableColumn propSize = new TableColumn("Size");
        TableColumn propOffset = new TableColumn("Offset");
        getColumns().addAll(propKey, propValue, propSize, propOffset);

        propKey.setPrefWidth(120);
        propKey.setCellValueFactory(new PropertyValueFactory<ByteItem, String>("key"));
        propKey.setSortable(false);

        propValue.setPrefWidth(200);
        propValue.setCellValueFactory(new PropertyValueFactory<ByteItem, String>("value"));
        propValue.setSortable(false);

        propSize.setPrefWidth(50);
        propSize.setCellValueFactory(new PropertyValueFactory<ByteItem, Integer>("size"));
        propSize.setSortable(false);

        propOffset.setPrefWidth(70);
        propOffset.setCellValueFactory(new PropertyValueFactory<ByteItem, Integer>("offset"));
        propOffset.setSortable(false);

        ObserverSystem.registerObserver(ObserverEvent.PAGE_SWITCH, this);
    }

    @Override
    public void update(Observable o, Object arg) {
        Publisher publisher = (Publisher) o;
        ObserverData transfer = ObserverData.transfer(arg);

        if (publisher.getObserverEvent() == ObserverEvent.PAGE_SWITCH) {
            Page page = (Page) transfer.getData();

            if (page instanceof PageIndex) {
                PageIndex pageIndex = (PageIndex) page;
                IndexPageHeader indexPageHeader = pageIndex.getIndexPageHeader();

                ObservableList<ByteItem> list = FXCollections.observableArrayList();

                int offset = 0;
                list.add(new ByteItem("N Dir Slots", String.valueOf(indexPageHeader.getnDirSlots()), 2, offset));
                list.add(new ByteItem("Heap Top", String.valueOf(indexPageHeader.getHeapTop()), 2, offset += 2));
                list.add(new ByteItem("N Heap", String.valueOf(indexPageHeader.getnHeap()), 2, offset += 2));
                list.add(new ByteItem("Free", String.valueOf(indexPageHeader.getFree()), 2, offset += 2));
                list.add(new ByteItem("Garbage", String.valueOf(indexPageHeader.getGarbage()), 2, offset += 2));
                list.add(new ByteItem("Last Insert", String.valueOf(indexPageHeader.getLastInsert()), 2, offset += 2));
                list.add(new ByteItem("Direction", String.valueOf(indexPageHeader.getDirection()), 2, offset += 2));
                list.add(new ByteItem("N Direction", String.valueOf(indexPageHeader.getnDirection()), 2, offset += 2));
                list.add(new ByteItem("N Recs", String.valueOf(indexPageHeader.getnRecs()), 2, offset += 2));
                list.add(new ByteItem("Max Trx ID", indexPageHeader.getMaxTransactionId(), 2, offset += 2));
                list.add(new ByteItem("Level", String.valueOf(indexPageHeader.getLevel()), 2, offset += 2));
                list.add(new ByteItem("Index ID", indexPageHeader.getIndexId(), 8, offset += 2));
                list.add(new ByteItem("BTree Seg Leaf", indexPageHeader.getBtrSegleaf(), 10, offset += 8));
                list.add(new ByteItem("BTree Seg Top", indexPageHeader.getBtrSegTop(), 10, offset += 10));
                setItems(list);
            } else if (page instanceof PageUndoLog) {
                PageUndoLog pageUndoLog = (PageUndoLog) page;
                UndoLogPageHeader undoLogPageHeader = pageUndoLog.getUndoLogPageHeader();

                ObservableList<ByteItem> list = FXCollections.observableArrayList();
            }
        }
    }
}
